/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QLNV.Action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.LookupDispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import java.util.*;
import QLNV.Entity.*;
import QLNV.Bean.*;
import java.sql.SQLException;

/**
 *
 * @author Ice
 */
public class NhanVienAction extends LookupDispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String VIEW = "view";
    private final static String DETAIL = "detail";

    /** Provides the mapping from resource key to method name.
     * @return Resource key / method name map.
     */
    protected Map getKeyMethodMap() {
        Map map = new HashMap();
        map.put("nhanvien.insert", "insert");
        map.put("nhanvien.delete", "delete");
        map.put("nhanvien.update", "update");
        map.put("nhanvien.view", "view");
        map.put("nhanvien.search", "search");
        map.put("nhanvien.detail", "detail");
        return map;
    }
    
    /**
     * get phong ban list
     * @param form
     * @return
     * @throws SQLException 
     */
    public ArrayList<PhongBan> getPbanList (ActionForm form) throws SQLException{
        PhongBanForm pBanForm = (PhongBanForm) form;
        return pBanForm.view();
    }
    
    public ArrayList<NhanVien> getnvList (ActionForm form) throws SQLException{
        NhanVienForm nvForm = (NhanVienForm) form;
        return nvForm.view();
    }
    /**
     * view
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception 
     */
    public ActionForward view(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        try {
            //NhanVienForm nvForm = (NhanVienForm) form;
            //ArrayList<NhanVien> nvList = new ArrayList<NhanVien>();
            //nvList = nvForm.view();
            request.setAttribute("nvList", getnvList(form));
            request.setAttribute("pBanList", getPbanList(new PhongBanForm()));
            request.setAttribute("errorMessage", "1");
        } catch (Exception e) {
            e.getMessage();
        }
        return mapping.findForward(VIEW); //forward to PhongBanInfo.jsp
    }
    
    /**
     * detail
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception 
     */
    public ActionForward detail(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        try {
            NhanVienForm nvForm = (NhanVienForm) form;
            ArrayList<NhanVien> nvList = new ArrayList<NhanVien>();
            nvList = nvForm.detail(nvForm.getManv());
            request.setAttribute("nvList", nvList);
            request.setAttribute("pBanList", getPbanList(new PhongBanForm()));
            request.setAttribute("errorMessage", "1");
        } catch (Exception e) {
            e.getMessage();
        }
        return mapping.findForward(DETAIL); //forward to PhongBanInfo.jsp
    }

    /**
     * insert
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception 
     */
    public ActionForward insert(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        //ArrayList<NhanVien> nvList = new ArrayList<NhanVien>();
        //ArrayList<PhongBan> pBanList = new ArrayList<PhongBan>();

        NhanVienForm nvForm = (NhanVienForm) form;
        //PhongBanForm pBanForm = (PhongBanForm) form;

        String manv = nvForm.getManv();
        String tennv = nvForm.getTennv();
        String mapban = nvForm.getMapban();
        String maluong = nvForm.getMaluong();
        String ngaysinh = nvForm.getNgaysinh();
        String gioitinh = nvForm.getGioitinh();
        String chucvu = nvForm.getChucvu();
        String noicongtac = nvForm.getNoicongtac();
        String diachi = nvForm.getDiachi();
        String email = nvForm.getEmail();

        String message = nvForm.insert(manv, mapban, maluong, tennv, gioitinh, ngaysinh, diachi, email, noicongtac, chucvu);
        //nvList = nvForm.view();
        //pBanList = pBanForm.view();
        request.setAttribute("errorMessage", message);
        request.setAttribute("nvList", getnvList(form));
        request.setAttribute("pBanList", getPbanList(new PhongBanForm()));
        return mapping.findForward(VIEW);
    }

    /**
     * update
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws SQLException 
     */
    public ActionForward update(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws SQLException {
        NhanVienForm nvForm = (NhanVienForm) form;
        //PhongBanForm pBanForm = (PhongBanForm) form;

        ArrayList<NhanVien> nvList = new ArrayList<NhanVien>();
        //ArrayList<PhongBan> pBanList = new ArrayList<PhongBan>();

        String manv = nvForm.getManv();
        String mapban = nvForm.getMapban();
        String maluong = nvForm.getMaluong();
        String tennv = nvForm.getTennv();
        String diachi = nvForm.getDiachi();
        String email = nvForm.getEmail();
        String noicongtac = nvForm.getNoicongtac();
        String chucvu = nvForm.getChucvu();
        String ngaysinh = nvForm.getNgaysinh();
        String gioitinh = nvForm.getGioitinh();
        //nvForm.update(manv, mapban, maluong, tennv, ngaysinh, gioitinh, chucvu, noicongtac, diachi, email);
        nvList = nvForm.update(manv, mapban, maluong, tennv, ngaysinh, gioitinh, chucvu, noicongtac, diachi, email);
        //pBanList = pBanForm.view();
        request.setAttribute("errorMessage","1");
        request.setAttribute("nvList", nvList);
        request.setAttribute("pBanList", getPbanList(new PhongBanForm()));
        
        return mapping.findForward(DETAIL);
    }

    /**
     * delete
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception 
     */
    public ActionForward delete(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        ArrayList<NhanVien> nvList = new ArrayList<NhanVien>();
        NhanVienForm nvForm = (NhanVienForm) form;
        String manv = nvForm.getManv();
        int cnt = nvForm.delete(manv);
        //if(cnt!=0){
        nvList = nvForm.view();
        request.setAttribute("nvList", nvList);
        request.setAttribute("errorMessage", "Delete successfully");
        //}
        return mapping.findForward(VIEW);
    }

    /**
     * search
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception 
     */
    public ActionForward search(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        ArrayList<NhanVien> nvList = new ArrayList<NhanVien>();
        NhanVienForm nvForm = (NhanVienForm) form;
        //PhongBan pBan = new PhongBan();
        String manv = nvForm.getManv();
        nvList = nvForm.search(manv);
        request.setAttribute("nvList", nvList);
        if (!nvList.isEmpty()) {
            request.setAttribute("errorMessage", "1");
            request.setAttribute("pBanList", getPbanList(new PhongBanForm()));
        } else {
            request.setAttribute("errorMessage", "Nhân viên này không tồn tại.");
            request.setAttribute("pBanList", getPbanList(new PhongBanForm()));
        }
        return mapping.findForward(VIEW);
    }
    /** Action called on Delete button click
     */
//    public ActionForward delete(ActionMapping mapping,
//            ActionForm form,
//            HttpServletRequest request,
//            HttpServletResponse response) throws java.lang.Exception {
//        // TODO:implement delete method
//        return mapping.findForward(SUCCESS);
    //}
    /* And your JSP would have the following format for submit buttons:
    
    <html:form action="/test">
    <html:submit property="method">
    <bean:message key="button.add"/>
    </html:submit>
    <html:submit property="method">
    <bean:message key="button.edit"/>
    </html:submit>
    <html:submit property="method">
    <bean:message key="button.delete"/>
    </html:submit>
    </html:form>
     */
}
